//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    encryptedData: '',
    isHide: false,
    nickName: '',
    avatarUrl: '',
    if_auth: 0,
    phone: '',
    is_grider: '',
    flag: 0,
    is_show:0
  },

  onLoad: function () {
    let phone = wx.getStorageSync('phoneNumber')
    if(phone){
      this.setData({
        phone:phone
      })
    }
  },

  onShow() {
    app.getUser()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    if (app.globalData.if_auth == 1) {
      this.setData({
        if_auth:1
      })
    } 
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/checkauth',
      data: {
        access_token: access_token
      },
      success: (res) => {
        console.log(res)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        }
        else if (res.data.code == 1) {
          this.setData({
            is_grider: res.data.is_grider,
            is_show:res.data.is_show
          })
        }
      }
    })
  },

  onHide() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: false
      })
    }
  },

  getUserInfo: function (e) {
    // 用户点击允许
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        if_auth:1,
        isHide: false
      })
      console.log(app.globalData.if_auth)
      let { encryptedData, iv, rawData, signature } = e.detail
      let access_token = wx.getStorageSync('access_token')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/auth',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          encryptedData: encryptedData,
          iv: iv,
          rawData: rawData,
          signature: signature,
          access_token: access_token
        },
        success: (res) => {
          console.log(res)
          wx.setStorageSync('userinfo', res.data.info)
        }
      })
    } else {
      console.log('用户点击拒绝授权')
    }
  },

  getPhoneNumber(e) {
    console.log(e)
    if (e.detail.errMsg = "getPhoneNumber:ok") {
      let { encryptedData, iv } = e.detail
      let access_token = wx.getStorageSync('access_token')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/phone',
        data: {
          encryptedData: encryptedData,
          iv: iv,
          access_token: access_token
        },
        success: (res) => {
          console.log(res)
          if (res.data.code == 1) {
            this.setData({
              phone: res.data.phone
            })
            wx.setStorageSync('phoneNumber', res.data.phone)
            wx.showToast({
              title: '手机号获取成功！',
            })
          }
        }
      })
    }
  },

  toActivity() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../my-activity/my-activity',
      })
    }
  },

  toGrid() {
    let phone = wx.getStorageSync('phoneNumber')
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else if(!phone) {
      wx.showToast({
        title: '请先获取手机号',
        icon:'none'
      })
    } else if(phone && this.data.is_grider == 0){
      wx.showToast({
        title: '您不是网格员，无法进入该模块',
        icon:'none'
      })
    } else {
      wx.navigateTo({
        url: '../grid-admin/grid-admin',
      })
    }
  },
  
 

  toPay() {
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../pay/pay',
      })
    }
  },

  toCamera(){
    if (app.globalData.if_auth == 0) {
      this.setData({
        isHide: true
      })
    } else {
      wx.navigateTo({
        url: '../my-camera/my-camera',
      })
    }
  },

  toParty(){
    wx.navigateToMiniProgram({
      appId: 'wxa27019ed76b1c8a2',
      success: (res) => {
        console.log(res)
      }
    })
  }
})
